package net.xdclass.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.request.CartItemRequest;
import net.xdclass.service.CartService;
import net.xdclass.utils.JsonData;
import net.xdclass.vo.CartItemVO;
import net.xdclass.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("購物車")
@RestController
@RequestMapping("/api/cart/v1")
public class CartController {
    @Autowired
    private CartService cartService;

    @ApiOperation("添加到購物車")
    @PostMapping("add")
    public JsonData addToCart(@RequestBody CartItemRequest cartItemRequest) {
        cartService.addToCart(cartItemRequest);

        return JsonData.buildSuccess();
    }

    @ApiOperation("獲取我的購物車")
    @GetMapping("mycart")
    public JsonData findMyCart() {
        CartVO cartVO = cartService.getMyCart();

        return JsonData.buildSuccess(cartVO);
    }

    @ApiOperation("清空購空車")
    @DeleteMapping("/clear")
    public JsonData cleanMyCart() {
        cartService.clear();
        return JsonData.buildSuccess();
    }

    @ApiOperation("刪除購物車項目")
    @DeleteMapping("/delete/{product_id}")
    public JsonData deleteItem(@ApiParam(value = "商品id", required = true) @PathVariable(value = "product_id") long productId) {
        cartService.deleteItem(productId);

        return JsonData.buildSuccess();
    }

    @ApiOperation("獲取對應訂單的商品信息")
    @PostMapping("confirm_order_cart_item")
    public JsonData confirmOrderCartItems(List<Long> productIdList) {
        List<CartItemVO> cartItemVOList = cartService.confirmOrderCartItems(productIdList);
        return JsonData.buildSuccess(cartItemVOList);
    }

    @ApiOperation("修改購物車數量")
    @PostMapping("change")
    public JsonData changeItemNum(@ApiParam(value = "商品數量修改", required = true)  @RequestBody CartItemRequest cartItemRequest) {
        cartService.changeItemNum(cartItemRequest);

        return JsonData.buildSuccess();
    }
}
